package com.hoperun.product.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.hoperun.distributecloth.R;
import com.hoperun.product.base.BaseActivity;
import com.hoperun.product.net.IServiceImpl;
import com.hoperun.product.net.RequestServiceImpl;
import com.hoperun.product.view.SimpleDialog;

import org.json.JSONObject;

public class TrayInfoActivity extends BaseActivity {

	private static final String EXTRA_REQUEST_CLOTHTKTNO = "TrayInfoActivity.clothTktNo";

	public static void actionStart(Context context, String clothTktNo) {
		Intent intent = new Intent(context, TrayInfoActivity.class);
		intent.putExtra(EXTRA_REQUEST_CLOTHTKTNO, clothTktNo);
		context.startActivity(intent);
	}

	private TextView clothTktNoTv;
	private TextView trayNoTv;
	private TextView listDateTv;
	private Button okBtn;
	private String mClothTktNo = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tray_info_activity);
		initData();
		initView();
		getTrayByTktNo(mClothTktNo);
	}

	private void initData() {
		mClothTktNo = getIntent().getStringExtra(EXTRA_REQUEST_CLOTHTKTNO);

	}

	private void initView() {
		clothTktNoTv = (TextView) findViewById(R.id.tray_info_clothTktNoTv);
		trayNoTv = (TextView) findViewById(R.id.tray_info_trayNoTv);
		listDateTv = (TextView) findViewById(R.id.tray_info_listDateTv);
		okBtn = (Button) findViewById(R.id.tray_info_okBtn);
		okBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				TrayInfoActivity.this.finish();
			}
		});

		clothTktNoTv.setText(mClothTktNo);

	}

	/**
	 * 根据三联单号带出托盘信息
	 * 
	 * @param clothTktNo
	 */
	private void getTrayByTktNo(String clothTktNo) {
		showWait();
		RequestServiceImpl.getTrayByTktNo(mMIPHandler, IServiceImpl.GET_TRAY_BY_TKTNO, clothTktNo);
	}

	@Override
	public void onPostHandle(int requestType, Object objBody, boolean error) throws Exception {
		String msg = "";
		String opt = "";
		if (error) {
			switch (requestType) {
			case IServiceImpl.GET_TRAY_BY_TKTNO:
				try {
					JSONObject jsonObj = new JSONObject((String) objBody);
					msg = jsonObj.optString("MSG_INFO");
					opt = jsonObj.optString("OPT_FLAG");
					if ("0".equals(opt)) {
						//JSONArray josnArray = jsonObj.optJSONArray("result");

						//if (josnArray != null && josnArray.length() > 0) {
							String trayNo = jsonObj.optString("TRAYNO");
							String listDate = jsonObj.optString("LISTDATE");
							trayNoTv.setText(trayNo);
							listDateTv.setText(listDate);

						//}

					} else if ("1".equals(opt)) {
						SimpleDialog.show(this, msg);
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
				break;

			}
		} else {
			switch (requestType) {
			case IServiceImpl.GET_TRAY_BY_TKTNO:
				if (objBody != null) {
					String obj = objBody.toString();
					SimpleDialog.show(this, obj);
				}
				break;

			}

		}
		stopWait();

	}

}
